package dhy.com.config;

import dhy.com.config.protocol.Serializer;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author zdh
 */
public abstract class Config {
    static Properties properties;

    //加载配置文件
    static {
        // xxx.class.getResourceAsStream("")--->以/开头,去类路径下找资源,否则去当前包下找资源
        //xxx.class.getClassLoader().getResourceAsStream("")--->只会去类路径下找资源
        try (InputStream in = Config.class.getResourceAsStream("/application.properties")) {
            properties = new Properties();
            properties.load(in);
        } catch (IOException e) {
            throw new ExceptionInInitializerError(e);
        }
    }


    /**
     * <P>
     *     读取配置文件中的端口信息,并返回
     * </P>
     * @return
     */
    public static int getServerPort() {
        String value = properties.getProperty("server.port");
        if(value == null) {
            //默认8080端口
            return 8080;
        } else {
            return Integer.parseInt(value);
        }
    }


    /**
     * <p>
     *     读取配置文件中序列化方式的配置
     * </p>
     * @return
     */
    public static Serializer.Algorithm getSerializerAlgorithm() {
        String value = properties.getProperty("serializer.algorithm");
        if(value == null) {
            //默认jdk序列化
            return Serializer.Algorithm.Java;
        } else {
            return Serializer.Algorithm.valueOf(value);
        }
    }
}